package cn.iocoder.yudao.module.vlog.history.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.vlog.history.dal.dataobject.VLogBrowseHistoryDO;
import cn.iocoder.yudao.module.vlog.history.service.VLogBrowseHistoryService;
import cn.iocoder.yudao.module.vlog.history.vo.VLogBrowseHistoryPageReqVO;
import cn.iocoder.yudao.module.vlog.info.dal.dataobject.VLogInfoDO;
import cn.iocoder.yudao.module.vlog.info.service.VLogInfoService;
import cn.iocoder.yudao.module.vlog.info.vo.AppVLogInfoRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 视频浏览记录")
@RestController
@RequestMapping("/vlog/browse-history")
@Validated
public class AppVLogBrowseHistoryController {
    @Resource
    private VLogBrowseHistoryService browseHistoryService;
    @Resource
    private VLogInfoService vLogInfoService;

    @GetMapping("/my-page")
    @Operation(summary = "获得我的视频浏览记录分页")
    public CommonResult<PageResult<AppVLogInfoRespVO>> getMyBrowseHistoryPage(@Valid SortablePageParam pageParam) {
        VLogBrowseHistoryPageReqVO pageReqVO = new VLogBrowseHistoryPageReqVO();
        pageReqVO.setPageNo(pageParam.getPageNo());
        pageReqVO.setPageSize(pageParam.getPageSize());
        pageReqVO.setUserId(getLoginUserId());
        pageReqVO.setUserDeleted(false);
        PageResult<VLogBrowseHistoryDO> pageResult = browseHistoryService.getBrowseHistoryPage(pageReqVO);
        Set<Long> vlogIds = convertSet(pageResult.getList(), VLogBrowseHistoryDO::getVlogId);
        List<VLogInfoDO> vlogInfoList = vLogInfoService.getVLogInfoList(vlogIds);
        List<AppVLogInfoRespVO> vLogInfoRespVOS = BeanUtils.toBean(vlogInfoList, AppVLogInfoRespVO.class);
        PageResult<AppVLogInfoRespVO> pageResultVO = new PageResult<>(vLogInfoRespVOS, pageResult.getTotal());
        return success(pageResultVO);
    }

}
